package com.clementheliou.cheliou.business.dataaccess.mark.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.clementheliou.cheliou.business.dataaccess.mark.MarkDataAccessService;
import com.clementheliou.cheliou.dal.dao.mark.MarkDAO;
import com.clementheliou.cheliou.dal.model.mark.Mark;

/**
 * {@link MarkDataAccessService} that used the default implementation provided
 * by the model's layer. It should not be used if many implementations are
 * availables in this last one.
 * <p>
 * Due to the {@link Transactional} annotation and the value of its
 * <i>readOnly</i> attribute, all methods are called in a transaction that will
 * be rollbacked in the end. This behavior can be overriden at the method level,
 * more particularly if you need to perform an insert, update or delete action.
 * </p>
 * 
 * @author Clément HELIOU (clement.heliou@gmail.com)
 * @see {@link MarkDataAccessService};
 * @see {@link Service};
 * @see {@link Transactional}.
 * @since 1.0
 */
@Service
@Transactional(readOnly = true)
public class MarkDefaultDataAccessService implements MarkDataAccessService {

	/**
	 * Logger for this class.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link Logger};
	 * @see {@link LoggerFactory}.
	 * @since 1.0
	 */
	private static final Logger LOGGER = LoggerFactory.getLogger(MarkDefaultDataAccessService.class);

	/**
	 * DAO used to access the entities of {@link Mark} type.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	@Autowired
	private MarkDAO markDAO;

	/**
	 * <i>{@inheritDoc}</i>
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link Transactional}.
	 * @since 1.0
	 */
	@Override
	@Transactional(readOnly = false)
	public void addMark(Mark markToBeAdded) {
		LOGGER.debug("Add the mark {} for the photo {} and the member {}.", new Object[] { markToBeAdded.getRank(), markToBeAdded.getMarkedPhoto().getId(), markToBeAdded.getAuthor().getId() });
		markDAO.saveEntity(markToBeAdded);
	}

	/**
	 * <i>{@inheritDoc}</i>
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	@Override
	public Mark getMarkOfMember(Integer photoId, Integer memberId) {
		LOGGER.debug("Get mark of member {} for the photo {}.", memberId, photoId);
		return markDAO.getMarkOfMember(photoId, memberId);
	}

	/**
	 * <i>{@inheritDoc}</i>
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	@Override
	public List<Integer> getMarksOfPhoto(Integer photoId) {
		LOGGER.debug("Get marks of photo {}.", photoId);
		return markDAO.getMarksOfPhoto(photoId);
	}

}
